<?php

/*
 * 缓存统一生成和清理
 */

namespace app\common\logic;

use think\Cache;

class MallCache
{
    /**
     * TAG缓存清理
     * @param type $tag
     */
    public static function clearTag($tag)
    {
        if ($tag === 'all') {
            Cache::clear();
        } else if ($tag) {
            if (!is_array($tag)) {
                $tag = [$tag];
            }

            foreach ($tag as $tag) {
                Cache::clear($tag);
            }
        }
        
        //重新生成一些静态缓存
        self::make($tag);
    }

    public static function make($tag)
    {
        if ($tag === 'all') {
            $tag = ['type', 'area', 'store', 'config', 'express'];
        }
        if (!is_array($tag)) {
            $tag = [$tag];
        }
        
        foreach ($tag as $tag) {
            switch ($tag) {
                //节点缓存
                case "type" :
                    $type = array_column(model("Common/Type")->getTypeByPid(0, 'tag'), 'tag');
                    foreach ($type as $type) {
                        \Fields::getById($type);
                    }
                    break;
                //地区缓存
                case "area" :
                    model("Common/Area")->getAreas();
                    break;
                //商城缓存
                case "store" :
                    break;
                //配置缓存
                case "config" :
                    \Config::lists();
                    break;
                //快递公司缓存
                case "express" :
                    \Express::lists();
                    break;
            }
        }
    }

}
